/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.controller.service;

import com.bice.controller.dao.Dao;
import com.bice.controller.entity.BiceUser;
import java.io.Serializable;
import javax.annotation.Resource;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sirius
 */
@Service
public class BiceUserService {
    
    @Resource
    private Dao<BiceUser, Serializable> dao;

    /**
     * Constructor of user service
     */
    public BiceUserService() {
    }
    
    /**
     * Save the biceUser
     * @param code Code of user
     * @param nickName Nickname of user
     * @param password Password cripted of user
     */
    public void save(String code, String nickName, String password) {
        BiceUser user = new BiceUser();
        user.setCode(code.trim());
        user.setNickname(nickName.trim());
        user.setPassword(password);
        dao.save(user);
    }
    
    /**
     * Get if exist user with specified nickname
     * @return <code>true</code>: BiceUser exists<br/><code>false</code>: Otherwise
     */
    public boolean isExistUserByNickName(String nickName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(BiceUser.class);
        criteria.add(Restrictions.eq("nickname", nickName));
        return dao.count(criteria) > 0;
    }
    
    /**
     * Get user by nickname and password
     * @param nickName nickname of user
     * @param password password of user
     * @return The user with nick and password
     */
    public BiceUser getUser(String nickName, String password) {
        DetachedCriteria criteria = DetachedCriteria.forClass(BiceUser.class);
        criteria.add(Restrictions.eq("nickname", nickName));
        criteria.add(Restrictions.eq("password", password));
        return dao.getUnique(criteria);
    }
    
    /**
     * Count all users
     * @return number of users
     */
    public int countUsers() {
        DetachedCriteria criteria = DetachedCriteria.forClass(BiceUser.class);
        return dao.count(criteria);
    }
    
}